<?php

/**
 * Description of ContentLogin
 *
 * @author Honza
 */
class ContentLogin implements IMainBodyWithInteraction {

  private $formMessage;

  public function action() {
    
  }

  public function applyForm($formID = NULL) {
    if ($_POST['login'] && $_POST['pass']) {
      //all fields filled

      $auth = Auth::getInstance();

      if ($auth->login($_POST['login'], $_POST['pass'])) {
        $identity = $auth->getIdentity();
        $login = $identity['login'];

        //redirect to index.php
        Header("Location:index.php");
      } else {
        $this->formMessage = "Wrong login information";
      }
    } else {
      $this->formMessage = "All fields are compulsory";
    }
  }

  public function getContent() {
    $content = '';
    $content .= '<h2>Login form</h2>';

    $formID = $this->getSubmitedForm();
    if ($formID > 0) {
      $this->applyForm($formID);
    }

    $content .= '<p class="error">' . $this->formMessage . '</p>';
    $content .= $this->getForm();
    
    return $content;
  }

  public function getForm($formID = NULL, $error = NULL) {
    $f = new Form();

    $f->startForm("self", "post", null, array("class" => "form_settings"));

    $auth = Auth::getInstance();
    if (!$auth->hasIdentity()) {
      $f->addLabel("Login", "login")->addInput("text", "login", $_POST['login']);
      $f->addLabel("Password", "pass")->addInput("password", "pass");
      $f->addLabel('&nbsp;', 'logme')->addInput("submit", 'logme', 'Log in', array("class" => "submit"));
    } else {
      $f->addLabel('&nbsp;', 'logmeout')->addInput("submit", 'logmeout', 'Log out', array("class" => "submit"));
    }

    $form .= $f->endForm()->getForm();

    return $form;
  }

  public function getSubmitedForm() {
    if (isset($_POST['logme'])) {
      return 1;
    }

    return -1;
  }

  public function getTable($tableID = NULL) {
    
  }

}

?>
